package de.hu.wbi.histoner.train;

/**
Copyright 2011,2012 Philippe Thomas
This file is part of histoner.

HistoNer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.

HistoNer  is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with HistoNer .  If not, see <http://www.gnu.org/licenses/>.
*/


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

import de.hu.wbi.histoner.HistoneModification;
/**
 * Class generates raw un-annotated pattern, by searching for modification terms, numbers, and amino acids from a file consisting of sentences.
 * The method extracts pattern from any kind of sentences, but it is recommended to use sentences dealing with histone modifications.
 * The used sentences are available on private communication. 
 * The format simply is a list of sentences delimited by new line
 * The resulting pattern are then manually annotated. 
 * @author philippe
 *
 */
public class RawPatternGenerator {
	
	public static String input =  "data/pattern/sentenes.txt.gz";
	public static String output = "data/pattern/pattern.raw";
	
	public static void main(String[] args) throws IOException {

		Pattern numberPattern = Pattern.compile("(\\D)(2A|2B|2AX|2BX|[1-9][0-9]{0,2})"); 
		System.out.println(HistoneModification.getModificationRegex());
		Pattern modificationPattern = Pattern.compile("(^|[>\\d\\s\\(\\)\\[\\]\\'\"/,;:\\-])"
				+"("+HistoneModification.getModificationRegex() +")"
				+"(?=([<\\.,\\s\\)\\(\\]\\[\\'\":;\\-?!/]|$))", //No numbering allowed after mod (me3 and so on)
				Pattern.CASE_INSENSITIVE);
		Pattern aaPattern = Pattern.compile("(^|[>\\d\\s\\)\\(\\[\\'\"/,;:\\-])" 
				+"(lysine|lys|arginine|arg|threonine|thr|serine|ser|glutamine|glutamatic acid|glu)"
						+"(?=([\\d<\\.,\\s\\)\\(\\]\\'\":;\\-?!/]|$))"
						,Pattern.CASE_INSENSITIVE);
		Pattern shortAAPattern= Pattern.compile("(^|[\\d>\\s\\(\\[\\'\"/,\\-])" +"(K|R|S|T|E)" +"(?=([0-9<\\.,\\s\\)\\]\\'\":;\\-?!/]|$))");
	    Pattern number = Pattern.compile("<number>.*<number>"); //We need at least two numbers for a histone modification
	    
		Matcher matcher  = null;
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(new GZIPInputStream( new FileInputStream(input)),"UTF8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		BufferedWriter bw = new BufferedWriter(new FileWriter(output));
		String text= null;
		try {
			while (( text= br.readLine()) != null){
			    StringBuffer sb= new StringBuffer(text);

			    //The order of the replacements have a direct influence! Don't change it
			    while((matcher = modificationPattern.matcher(sb.toString())).find()){
			    	sb.replace(matcher.start(2), matcher.end(2), "<mod>");
			    }
			    
			    while((matcher = numberPattern.matcher(sb.toString())).find())
			    	sb.replace(matcher.start(2), matcher.end(2), "<number>");
			    
			    while((matcher = aaPattern.matcher(sb.toString())).find())
			    	sb.replace(matcher.start(2), matcher.end(2), "<aa>");
			    
			    while((matcher = shortAAPattern.matcher(sb.toString())).find())
			    	sb.replace(matcher.start(2), matcher.end(2), "<aa>");	         
			    
			    text= sb.toString();		    

			    if (text.contains("<aa>") == false)
			    	continue;
			    
			    if(text.contains("<mod>") == false)
			    	continue;
			        		    
			    if(!number.matcher(text).find())
			    	continue;

			    bw.append(text.substring(text.indexOf("<"), text.lastIndexOf(">")+1) +"\n");
			
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		bw.close();
	}

}
